package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装为Request对象（根据InputSteam输入流封装）
 */
public class Request {

    private String method; // 请求方式，比如GET/POST
    private String url;  // 例如 /,/index.html
    private InputStream inputStream;  // 输入流，其他属性从输入流中解析出来

    private String context;
    private String wrapper;

    private String hots;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getHots() {
        return hots;
    }

    public void setHots(String hots) {
        this.hots = hots;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getWrapper() {
        return wrapper;
    }

    public void setWrapper(String wrapper) {
        this.wrapper = wrapper;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }


    // 构造器，输入流传入
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String message = new String(bytes);
//        System.out.println(message);

        //localhost:8080/web-demo02/resume/addresume02
        //localhost1:8080/web-demo03/null
        //GET /web-demo02/resume/addresume02 HTTP/1.1
        //Host: localhost:8080
        //Connection: keep-alive
        //Cache-Control: max-age=0
        //Upgrade-Insecure-Requests: 1
        //User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
        //Sec-Fetch-User: ?1
        //Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
        //Sec-Fetch-Site: none
        //Sec-Fetch-Mode: navigate
        //Accept-Encoding: gzip, deflate, br
        //Accept-Language: zh-CN,zh;q=0.9

        String[] messageArray = message.split("\\n");

        // 第一行
        String[] firstLine = messageArray[0].split(" ");

        this.method = firstLine[0];
        this.url = firstLine[1]; // /web-demo02/lagou/haha/index.html

        String[] urlArray = firstLine[1].substring(1).split("/");
        this.context = urlArray[0]; // web-demo02

        StringBuffer wrapperBuffer = new StringBuffer();
        for (int i = 0; i < urlArray.length; i ++) {
            if (i == 0) continue;
            wrapperBuffer.append("/" + urlArray[i]);
        }
        this.wrapper = wrapperBuffer.toString();

        // 第二行 Host: localhost:8080
        String[] secondLine = messageArray[1].split(" ");

        this.hots = secondLine[1].split(":")[0];
    }
}
